package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrBean;
import com.koron.css2.baseConfig.mapper.MatrMapper;
import com.koron.css2.baseConfig.queryBean.MatrQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 材料价格-编辑初始化
 *
 * @author 廖大富
 */
public class MatrQuery implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MatrQuery.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<MatrBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", MatrBean.class);

        try {
            MatrMapper mapper = factory.getMapper(MatrMapper.class);
            MatrQueryBean bean = JsonUtils.objectToPojo(req.getData(), MatrQueryBean.class);
            if (StringUtils.isEmpty(bean.getMatrNo())){
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料编码不能为空", void.class);
            }
            MatrBean matrBean = mapper.selectMatrByMatrNo(bean.getMatrNo());
            info.setData(matrBean);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }
}
